import request from '@/utils/request'
import type { PageDto, PageReqDto } from '../types'
import type { AllocateRecordDto, ProductDto, SaleReqDto } from './types'


/**
 * 新增销售
 *
 * @param data 添加联系人的请求数据
 * @returns 返回包含操作结果的 Promise 对象，操作成功时返回字符串类型的操作结果
 */
export function addContact(data: Omit<SaleReqDto, 'saleOldId'>): Promise<string> {
    return request.put('/crm-prod/crm/deal/set-contact', data)
}


/**
 * 更新销售
 *
 * @param data 包含更新联系人信息的请求体和旧的销售ID
 * @returns 返回操作结果的Promise对象，解析为字符串类型
 */
export function editContact(data: SaleReqDto): Promise<string> {
    return request.put('/crm-prod/crm/deal/edit-contact', data)
}

/**
 * 根据saleId获取销售下的签单信息
 * @param params 
 * @returns 
 */
export function getProductBySale(params: {
    saleId: number
}): Promise<ProductDto[]> {
    return request.get('/crm-prod/crm/deal/product-by-sale', {
        params
    })
}

export function getSaleContract(params: {
    deptId: number,
    saleId: number,
    productType: number,
    companyName: string,
    sortField: string,
    sortOrder: string,
} & PageReqDto): Promise<PageDto<ProductDto[]>> {
    return request.get('/crm-prod/crm/deal/list', {
        params
    })
}

/**
 * 离职继承签单分配
 * @param params 
 * @returns 
 */
export function handleAllocate(data: {
    saleId: number
    saleOldId: number
    companyIds: number[]
    serviceStartTime: string
}): Promise<void> {
    return request.post('/crm-prod/crm/deal/deal-allocate', data)
}

/**
 * 获取分配记录
 * @param data 
 * @returns 
 */
export function getAllocateRecord(params: {
    saleOldId: number
    saleId?: number
    companyName?: string
}): Promise<AllocateRecordDto[]> {
    return request.get('/crm-prod/crm/deal/allocate-record', {
        params
    })
}

